//
//  ChangeAddressViewController.m
//  LineMoney
//
//  Created by 尹宁 on 16/7/19.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ChangeAddressViewController.h"
#import "ChangePasswordTableViewCell.h"
@interface ChangeAddressViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;


@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *pickTopView;

@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *mounthArr;
@property (nonatomic, strong) NSMutableArray *dayArr;

@property (nonatomic, strong) NSString *str;
@end

@implementation ChangeAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改地址";
    _arr = [NSMutableArray array];
    [self creatTableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}


- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, KSCREENWIDTH, KSCREENHEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ChangePasswordTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.sectionFooterHeight = 0;
    
}

#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    if (_pickView == nil) {
        self.yearArr = [NSMutableArray arrayWithObjects:@"辽宁省大连市甘井子区",@"吉林省吉林市白城区",@"黑龙江省哈尔滨市市漠河区", nil];
        self.mounthArr = [NSMutableArray arrayWithObjects:@"7",@"8",@"9",@"10", nil];
        self.dayArr = [NSMutableArray arrayWithObjects:@"15",@"16",@"17",@"18", nil];
        
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
        _pickView.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView.delegate = self;
        _pickView.dataSource = self;
        [self.view addSubview:_pickView];
        _pickView.backgroundColor = [UIColor huiseColor];
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;
        
    }else{
        [self.view addSubview:_pickView];
        [self.view addSubview:_pickTopView];
    }
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView removeFromSuperview];
        [_pickTopView removeFromSuperview];
        
        
        ChangePasswordTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        cell.InputTextfield.text = _str;
    }];
    
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //    if (component == 0) {
    //        return _yearArr[row];
    //    }else if (component == 1){
    //        return _mounthArr[row];
    //    }else{
    //
    //        return _dayArr[row];
    //    }
    return _yearArr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _str = _yearArr[row];
}


#pragma mark - TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChangePasswordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.SmallLabel.textColor = BACKGROUNDCOLOR;
    cell.LineLabel.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:238 / 255.0 alpha:1];
    cell.ChangeButton.tintColor = BACKGROUNDCOLOR;
    [[[cell.ChangeButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        [self creatPickerView:cell.ChangeButton];
    }];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [_arr addObject:cell.InputTextfield];
    cell.InputTextfield.delegate = self;
    cell.InputTextfield.tag = indexPath.row + 1000;
    
    if (indexPath.row == 0) {
        cell.SmallLabel.text = @"所在地区";
        cell.InputTextfield.text = @"辽宁省大连市甘井子区";
    } else  {
        cell.SmallLabel.text = @"详细地址";
        cell.InputTextfield.text = @"大有恬园二期F15-201";
        cell.SmallLabel.text = @"所在地区";
        [cell.InputTextfield becomeFirstResponder];
        cell.LineLabel.backgroundColor = BACKGROUNDCOLOR;
        cell.ChangeButton.hidden = YES;
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    for (UITextField *text in _arr) {
        if (textField == text) {
            ChangePasswordTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag - 1000 inSection:0]];
            cell.LineLabel.backgroundColor = BACKGROUNDCOLOR;
        } else {
            ChangePasswordTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:text.tag - 1000 inSection:0]];
            cell.LineLabel.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:238 / 255.0 alpha:1];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
